<template>
  <div class="app-container">
    <!-- 搜索配置   -->
    <div class="search-container" >
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item>
          <el-button type="primary" :disabled="queryParams.tenantId===undefined || queryParams.tenantId===null"
                     @click="handleQuery">
            <i-ep-search/>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <i-ep-refresh/>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
      >
        <el-table-column label="ID" prop="id"/>
        <el-table-column label="配置名" prop="confName"/>
        <el-table-column label="配置key" prop="confKey"/>
        <el-table-column label="配置value" prop="confValue"/>
        <el-table-column label="操作" fixed="right" align="left" width="200">
          <template #default="scope">
            <el-button
              type="primary"
              link
              size="small"
              @click.stop="handleUpdate(scope.row)"
            >
              <i-ep-edit/>
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--配置弹窗-->
    <el-drawer
      v-model="dialog.visible"
      :title="dialog.title"
      size="30%"
      @close="handleCloseDialog"
    >
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="computedRules"
        label-width="100px"
        :inline="true"
      >
        <el-form-item label="名称" prop="confName">
          <el-input
            v-model="formData.confName"
            disabled
            placeholder="请输入名称"
          />
        </el-form-item>
        <el-form-item label="key" prop="confKey">
          <el-input v-model="formData.confKey" disabled placeholder="key"/>
        </el-form-item>
        <el-form-item label="配置值" prop="confValue">
          <el-input v-model="formData.confValue" placeholder="请输入配置值"/>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmitClick">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>
<script setup lang="js">
import {getPage, postConfig} from "@/api/config";

defineOptions({
  name: "Config",
  inherititems: false,
});

const queryFormRef = ref(ElForm);

const loading = ref(false);

const queryParams = reactive({
  agentId: null,
  tenantId: null,
});

// 配置表格数据
const pageData = ref();

/** 查询 */
function handleQuery() {
  loading.value = true;
  getPage(queryParams)
    .then((data) => {
      pageData.value = data;
    })
    .finally(() => {
      loading.value = false;
    });
}

const computedRules = computed(() => {
  return {
    confValue: [
      {required: true, message: "请输入配置的值", trigger: "blur"},
    ],
  };
});
const dialog = reactive({
  title: "",
  visible: false,
});
const formData = ref({});

function handleUpdate(row) {
  dialog.visible = true;
  formData.value = {};
  formData.value.confName = row.confName;
  formData.value.confKey = row.confKey;
  formData.value.confValue = row.confValue;
}

function handleSubmitClick() {
  dataFormRef.value.validate((isValid) => {
    if (isValid) {
      loading.value = true;
      let d = {
        tenantId: queryParams.tenantId,
        key: formData.value.confKey,
        value: formData.value.confValue,
      };
      postConfig(d)
        .then((data) => {
          handleQuery();
        })
        .finally(() => {
          dialog.visible = false;
        });
    }
  });
}

const dataFormRef = ref(ElForm);

/** 关闭弹窗 */
function handleCloseDialog() {
  dialog.visible = false;

  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();

  formData.value.id = undefined;
}

/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}
onMounted(() => {
  handleQuery();
});
</script>

<style scoped lang="scss"></style>
